﻿using JNPF.Basics.Logic;
using JNPF.Basics.Models;
using JNPF.Utils;
using JNPF.Utils.Controller;
using Microsoft.AspNetCore.Mvc;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace JNPF.Basics.Controller
{
    /// <summary>
    /// 地图管理
    /// 版 本：V3.0.0
    /// 版 权：引迈信息技术有限公司
    /// 作 者：管理员/admin
    /// 日 期：2020-08-03 11:49
    /// </summary>
    [Route("api/DataV/[controller]")]
    public class MapController : BaseApiController
    {
        private MapBll mapBll = new MapBll();
        private UserBll userBll = new UserBll();

        #region GET
        /// <summary>
        /// 列表
        /// </summary>
        /// <param name="input">请求参数</param>
        /// <returns>返回列表</returns>
        [HttpGet]
        public async Task<ResponseResult> GetList([FromQuery] PageInput input)
        {
            var data = await mapBll.GetList(input);
            var output = new PageResult()
            {
                list = data.list.Select(x => new MapListOutput()
                {
                    creatorTime = x.CreatorTime,
                    creatorUser = userBll.GetUserName(x.CreatorUserId),
                    enCode = x.EnCode,
                    enabledMark = x.EnabledMark,
                    fullName = x.FullName,
                    id = x.Id,
                    sortCode = x.SortCode
                }),
                pagination = data.pagination
            };
            return Success(output);
        }

        /// <summary>
        /// 下拉列表
        /// </summary>
        [HttpGet("Selector")]
        public async Task<ResponseResult> GetSelector()
        {
            var data = await mapBll.GetList();
            var output = data.MapToList<MapSelectorOutput>();
            return Success(new { list = output });
        }

        /// <summary>
        /// 信息
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns>返回对象</returns>
        [HttpGet("{id}")]
        public async Task<ResponseResult> GetInfo(string id)
        {
            var data = (await mapBll.GetInfo(id)).MapTo<MapInfoOutput>();
            return Success(data);
        }

        /// <summary>
        /// 获取地图数据
        /// </summary>
        /// <param name="id">id</param>
        /// <returns>返回列表</returns>
        [HttpGet("{id}/Data")]
        public async Task<object> GetMapData(string id)
        {
            var entity = await mapBll.GetInfo(id);
            if (entity != null)
            {
                return entity.Data.ToObject();
            }
            return new { data = new List<object>() };
        }
        #endregion

        #region POST
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        [HttpDelete("{id}")]
        public async Task<ResponseResult> Delete(string id)
        {
            var entity = await mapBll.GetInfo(id);
            if (entity != null)
            {
                var isOk = await mapBll.Delete(entity);
                return isOk > 0 ? Success("删除成功") : Error("删除失败");
            }
            else
            {
                return Error("数据不存在");
            }

        }

        /// <summary>
        /// 创建
        /// </summary>
        /// <param name="input">实体对象</param>
        /// <returns></returns>
        [HttpPost]
        public async Task<ResponseResult> Create([FromBody] MapCrInput input)
        {
            var entity = input.MapTo<MapEntity>();
            var isOk = await mapBll.Create(entity);
            return isOk > 0 ? Success("创建成功") : Error("创建失败");
        }

        /// <summary>
        /// 编辑
        /// </summary>
        /// <param name="id">主键值</param>
        /// <param name="input">实体对象</param>
        /// <returns></returns>
        [HttpPut("{id}")]
        public async Task<ResponseResult> Update(string id, [FromBody] MapUpInput input)
        {
            var entity = input.MapTo<MapEntity>();
            var isOk = await mapBll.Update(id, entity);
            return isOk > 0 ? Success("更新成功") : Error("更新失败");
        }

        /// <summary>
        /// 更改状态
        /// </summary>
        /// <param name="id">主键值</param>
        /// <returns></returns>
        [HttpPut("{id}/Actions/State")]
        public async Task<ResponseResult> ActionsState(string id)
        {
            var entity = await mapBll.GetInfo(id);
            entity.EnabledMark = entity.EnabledMark == 1 ? 0 : 1;
            var isOk = await mapBll.Update(id, entity);
            return isOk > 0 ? Success("更新成功") : Error("更新失败");
        }
        #endregion
    }
}
